/**
 * created by 小卷毛, 2021-06-26
 * Copyright (c) 2021, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.rzico.sbl.viewmodel

import com.lzg.extend.BaseResponse
import com.lzg.extend.JsonConvert
import com.lzg.extend.jackson.JacksonConvert
import com.lzg.okrx.RxSingleHelper
import com.lzg.okrx.adapter.FlowableBody
import com.lzy.okgo.OkGo
import com.rzico.sbl.model.BaseUrl
import com.rzico.sbl.model.CommonData
import com.rzico.sbl.model.CouponData
import com.rzico.sbl.model.ResponseModel
import com.xinnuo.common_ui.utils.applyFlowableSchedulers
import com.xinnuo.common_ui.utils.applySchedulers

/**
 * 描 述：电子水票操作相关
 * 作 者：小卷毛  2021-06-26 16:52
 * 修改描述：
 * 修 改 人：
 * 修改版本：
 */
class TicketViewModel : CommonViewModel() {

    /**
     * 电子水票列表
     */
    fun ticketList(cardId: String) =
        OkGo.get<BaseResponse<ArrayList<CommonData>>>(BaseUrl.ticketCount)
            .params(
                getParams(
                    "cardId" to cardId,
                    "subType" to "water",
                )
            )
            .converter(object : JacksonConvert<BaseResponse<ArrayList<CommonData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }

    /**
     * 优惠券列表
     */
    fun couponList(cardId: String) =
        OkGo.get<BaseResponse<ArrayList<CouponData>>>(BaseUrl.ticketCount)
            .params(
                getParams(
                    "cardId" to cardId,
                    "subType" to "none",
                )
            )
            .converter(object : JacksonConvert<BaseResponse<ArrayList<CouponData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }

    /**
     * 水票品牌列表
     */
    fun ticketPdt() =
        OkGo.get<BaseResponse<ResponseModel<CommonData>>>(BaseUrl.ticketPdt)
            .params(
                getParams(
                    "type" to "query",
                    "subType" to "water",
                    "pageStart" to 0,
                    "pageSize" to 200
                )
            )
            .converter(object : JsonConvert<BaseResponse<ResponseModel<CommonData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }
            .map { it.data.data }

    /**
     * 赠送水票 BaseUrl.ticketAdd
     * 核销水票 BaseUrl.ticketUse
     */
    fun ticketEidt(url: String, vararg params: Pair<String, Any>) =
        RxSingleHelper.requestString(
            url = url,
            isMultipart = true,
            params = getParams(*params)
        )
            .compose(applySchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

    /**
     * 删除优惠券
     */
    fun couponDel(couponId: String) =
        RxSingleHelper.requestString(
            url = BaseUrl.couponDel,
            params = getParams("id" to couponId)
        )
            .compose(applySchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

    /**
     * 水票记录列表
     */
    fun ticketRcd(
        page: Int,
        ticketId: String
    ) =
        OkGo.get<BaseResponse<ResponseModel<CommonData>>>(BaseUrl.ticketRcd)
            .params(
                getParams(
                    "id" to ticketId,
                    "pageStart" to page * 20,
                    "pageSize" to 20
                )
            )
            .converter(object : JsonConvert<BaseResponse<ResponseModel<CommonData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data.data }

}